import { useUserInfoStore } from '@/path/store/userInfo';
import { router } from "./index"

const unLoginPageList = [
  "/pages/tabbar/index/index",
  "/pages/tabbar/buy/index",
  "/pages/tabbar/mine/index",
  "/pages/mine/login/index",
]

/**路由拦截器 */
export default [
  function (ctx, next, error) {
    const routerItem = unLoginPageList.find(v => v === ctx.router)
    if (routerItem) {
      next()
    } else {
      const userStore = useUserInfoStore()
      if (userStore.userInfo?.token) {
        next()
      } else {
        uni.showModal({
          title: "温馨提示",
          confirmText: "去登录",
          content: "您还没有登录，请先登录后再使用此功能。",
          success(res) {
            if (res.cancel) return error()
            router.navigate({
              url: "/pages/mine/login/index"
            }).then((res) => {
              if (res != null) {
                next()
              } else {
                error()
              }
            })
          },
          fail() {
            error()
          }
        })
      }
    }
  },
]